<?php

include_once "src/regexception/GeneralRegistryException.php";
include_once "src/model/RegistryCount.php";

class EmailManager {

    const REPLY_TO = 'noreply@txssc.txstate.edu';

    public static function sendPasswordRecoveryEmail($firstName, $lastName, $email, $password) {
        try {
            $to  = $firstName . ' ' . $lastName . ' <' . $email . '>';
            $subject = 'Account Password Recovery';
            $passwordRecoveryEmailContent = EmailManager::generatePasswordRecoveryEmailContent($firstName, $lastName, $email, $password);
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headers .= 'From: Safety & Security Consultant Registry of Texas <' . self::REPLY_TO . '>' . "\r\n";
            if (!mail($to, $subject, $passwordRecoveryEmailContent, $headers)) {
                throw new GeneralRegistryException("SendingEmailException");
            }           
        } catch (GeneralRegistryException $e) {
            throw $e;
        }
    }

    private static function generatePasswordRecoveryEmailContent($firstName, $lastName, $email, $password) {
        $passwordRecoveryEmailContent = '<p>
                                            Hi '. $firstName . ' ' . $lastName .',
                                         </p>
                                         <p>
                                            We have received a request to recover your
                                            account password and/or email for the Safety & Security
                                            Consultant Registry of Texas. 
                                            The password for your account is listed below.
                                         </p>
                                         <p>
                                            <b>Login: </b> ' . $email . '<br />
                                            <b>Password: </b> ' . $password . '
                                        </p>
                                        <p>
                                            To access the registry click here:
                                            <a href="https://registry.txssc.txstate.edu">https://registry.txssc.txstate.edu</a>
                                        </p>
                                        <p>
                                            This is an automated message, please do not reply to this email.
                                            If you are still having problems accessing your account,
                                            please contact us at (877) 304-2727 or by email at <a href="mailto:txssc@txstate.edu">txssc@txstate.edu</a>.
                                        </p>';
        return $passwordRecoveryEmailContent;
    }

    public static function sendUserAccountDeletionEmail($firstName, $lastName, $email) {
        try {
            $to  = $firstName . ' ' . $lastName . ' <' . $email . '>';
            $subject = 'Account Deletion Confirmation';
            $passwordRecoveryEmailContent = EmailManager::generateUserAccountDeletionEmailContent($firstName, $lastName, $email);
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headers .= 'From: Safety & Security Consultant Registry of Texas <' . self::REPLY_TO . '>' . "\r\n";
            if (!mail($to, $subject, $passwordRecoveryEmailContent, $headers)) {
                throw new GeneralRegistryException("SendingEmailException");
            }
        } catch (GeneralRegistryException $e) {
            throw $e;
        }
    }

    private static function generateUserAccountDeletionEmailContent($firstName, $lastName, $email) {
        $userAccountDeletionEmailContent = '<p>
                                                '. $firstName . ' ' . $lastName .',
                                            </p>
                                            <p>
                                                The request to delete your consulting registry account has been processed.
                                                Your account and public profile has been completely removed from the system.
                                            </p>
                                            <p>
                                                To access the registry click here:
                                                <a href="https://registry.txssc.txstate.edu">https://registry.txssc.txstate.edu</a>
                                            </p>
                                            <p>
                                                This is an automated message, please do not reply to this email.
                                                If you have any more questions regarding your account deletion,
                                                please contact us at (877) 304-2727 or by email at <a href="mailto:txssc@txstate.edu">txssc@txstate.edu</a>.
                                            </p>
                                            <p>
                                                Thank you,<br />
                                                Texas School Safety Center.
                                            </p>';
        return $userAccountDeletionEmailContent;
    }

    public static function sendUserAccountCreationConfirmationEmail($firstName, $lastName, $email, $password) {
        try {
            $to  = $firstName . ' ' . $lastName . ' <' . $email . '>';
            $subject = 'Account Creation Confirmation';
            $passwordRecoveryEmailContent = EmailManager::generateUserAccountCreationConfirmationEmailContent($firstName, $lastName, $email, $password);
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headers .= 'From: Safety & Security Consultant Registry of Texas <' . self::REPLY_TO . '>' . "\r\n";
            if (!mail($to, $subject, $passwordRecoveryEmailContent, $headers)) {
                throw new GeneralRegistryException("SendingEmailException");
            }
        } catch (GeneralRegistryException $e) {
            throw $e;
        }
    }

    private static function generateUserAccountCreationConfirmationEmailContent($firstName, $lastName, $email, $password) {
        $passwordRecoveryEmailContent = '<p>
                                            Hi '. $firstName . ' ' . $lastName .',
                                         </p>
                                         <p>
                                            Your account within the Safety & Security
                                            Consultant Registry of Texas has been successfully created.
                                         </p>
                                         <p>
                                            Please keep the following login information for your records.
                                         </p>
                                         <p>
                                            <b>Email: </b> ' . $email . '
                                            <br />
                                            <b>Password: </b> ' . $password . '
                                        </p>
                                        <p>
                                            To access the registry click here:
                                            <a href="https://registry.txssc.txstate.edu">https://registry.txssc.txstate.edu</a>
                                        </p>
                                        <p>
                                            This is an automated message, please do not reply to this email.
                                            If you have any problem accessing your account,
                                            please contact us at (877) 304-2727 or by email at <a href="mailto:txssc@txstate.edu">txssc@txstate.edu</a>.
                                        </p>';
        return $passwordRecoveryEmailContent;
    }

    public static function sendServiceCategoryToBeAddedEmail($email, $categoryName, $categoryDescription) {
        try {
            $to  = 'Research Department <txssc-research@txstate.edu>';
            $subject = 'Service Category Request';
            $passwordRecoveryEmailContent = EmailManager::generateServiceCategoryToBeAddedEmailContent($email, $categoryName, $categoryDescription);
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headers .= 'From: Safety & Security Consultant Registry of Texas <' . self::REPLY_TO . '>' . "\r\n";
            if (!mail($to, $subject, $passwordRecoveryEmailContent, $headers)) {
                throw new GeneralRegistryException("SendingEmailException");
            }
        } catch (GeneralRegistryException $e) {
            throw $e;
        }
    }

    private static function generateServiceCategoryToBeAddedEmailContent($email, $categoryName, $categoryDescription) {
        $passwordRecoveryEmailContent = '<p>
                                            The following service category addition has been requested:
                                         </p>
                                         <p>
                                            <b>Email: </b> ' . $email . '
                                            <br /><br />
                                            <b>Category Name: </b> ' . $categoryName . '
                                            <br /><br />
                                            <b>Category Description: </b> ' . $categoryDescription . '
                                        </p>';
        return $passwordRecoveryEmailContent;
    }

    public static function sendRegistryCountsReportEmail($email, $registryCount) {
        try {
            $to  = $email . ' <' . $email . '>';
            $subject = 'Registry Counts Report';
            $registryCountsReportEmailContent = EmailManager::generateRegistryCountsReportEmailContent($email, $registryCount);
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headers .= 'From: Safety & Security Consultant Registry of Texas <' . self::REPLY_TO . '>' . "\r\n";
            if (!mail($to, $subject, $registryCountsReportEmailContent, $headers)) {
                throw new GeneralRegistryException("SendingEmailException");
            }
        } catch (GeneralRegistryException $e) {
            throw $e;
        }
    }

    private static function generateRegistryCountsReportEmailContent($email, RegistryCount $registryCount) {
        $passwordRecoveryEmailContent = '<p>
                                            Registry Counts Report:
                                         </p>
                                         <p>
                                            <b>Total Accounts Created: </b> ' . $registryCount->getTotalAccountsCreated() . '
                                            <br /><br />
                                            <b>Total Profiles Published: </b> ' . $registryCount->getTotalProfilesPublished() . '
                                            <br /><br />
                                            <b>Total Profiles Not Published: </b> ' . $registryCount->getTotalProfilesNotPublished() . '
                                            <br /><br />
                                            <b>Total K12 Profiles Published: </b> ' . $registryCount->getTotalK12ProfilesPublished() . '
                                            <br /><br />
                                            <b>Total Higher Education Profiles Published: </b> ' . $registryCount->getTotalHigherEducationProfilesPublished() . '
                                            <br /><br />
                                            <b>Total K12 Unique Profiles Published: </b> ' . $registryCount->getTotalK12UniqueProfilesPublished() . '
                                            <br /><br />
                                            <b>Total Higher Education Profile Published: </b> ' . $registryCount->getTotalHigherEducationUniqueProfilesPublished() . '
                                        </p>';
        return $passwordRecoveryEmailContent;
    }

    public static function sendMassCommunicationMessageEmail($title, $message, $users) {
        try {
            //$to  = 'Fabio Canache' . ' <fcanache@gmail.com>';
            $subject = 'Registry Counts Report';
            $massCommunicationMessageEmailContent = EmailManager::generateMassCommunicationMessageEmailContent($title, $message);
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headers .= 'From: Safety & Security Consultant Registry of Texas <' . self::REPLY_TO . '>' . "\r\n";
            for ($i = 0; $i < count($users); $i++) {
                sleep(1);
                if (!mail($users[$i], $subject, $massCommunicationMessageEmailContent, $headers)) {
                    throw new GeneralRegistryException("SendingEmailException");
                }
            }
        } catch (GeneralRegistryException $e) {
            throw $e;
        }
    }

    private static function generateMassCommunicationMessageEmailContent($title, $message) {
        $passwordRecoveryEmailContent = '<p>
                                            ' . $title . '
                                         </p>
                                         <p>
                                            ' . $message . '
                                        </p>';
        return $passwordRecoveryEmailContent;
    }

}
?>
